'''
Data classes for the splash screens
'''

from PyQt4 import QtCore
from rgeSave import *
import os
from rgeSystem import *
import rgeExtra
import rgeTileset
import rgeViews
import rgeBase

#standard data keys
NUM_OF_LAYERS_KEY = "_number_of_layers"
LAYERS_KEY = "_layers"
WIDTH_KEY = "_width"
HEIGHT_KEY = "_height"
TILES_KEY = "_tiles"

#standard data indexes
#int
NUM_OF_LAYERS_INDEX = 0

#int vec
TILES_INDEX = 0
WIDTH_INDEX = 1
HEIGHT_INDEX = 2

#tileset vec
LAYERS_INDEX = 0




class SceneData(rgeBase.BaseData):
    def __init__(self, NewName = "No_Name"):
        super(SceneData, self).__init__(NewName)
        self.addStandard(rgeExtra.IntItem(20, NUM_OF_LAYERS_KEY, NUM_OF_LAYERS_INDEX))
        self.addStandard(rgeExtra.TilesetVecItem("", LAYERS_KEY, LAYERS_INDEX, 1, [20]))
        self.addStandard(rgeExtra.IntVecItem(0, TILES_KEY, TILES_INDEX, 2, [20, 0]))
        self.addStandard(rgeExtra.IntVecItem(1, WIDTH_KEY, WIDTH_INDEX, 1, [20]))
        self.addStandard(rgeExtra.IntVecItem(1, HEIGHT_KEY, HEIGHT_INDEX, 1, [20]))

        self.setFilename(SCENE_DIR + '/' + NewName + SCENE_EXTENSION)


    def getNumOfLayers(self):
        return self.getStandard(NUM_OF_LAYERS_KEY).getValue()

    def getWidth(self, Index):
        return self.getStandard(WIDTH_KEY).getValue([Index])

    def getHeight(self, Index):
        return self.getStandard(HEIGHT_KEY).getValue([Index])

    def getLayer(self, Index):
        return self.getStandard(LAYERS_KEY).getValue([Index])

    def getTiles(self, Index):
        return self.getStandard(TILES_KEY).getValueList([Index])

    def getTileset(self, Index):
        ReturnValue = rgeTileset.TilesetData()
        Filepath = self.getLayer(Index)
        if len(Filepath) > 0:
            ReturnValue.loadFromFile(Filepath)
        return ReturnValue

    def setLayer(self, tileset, layerNum):
        if not tileset:
            tileset = ""
        if type(tileset) is not str:
            tileset = tileset.getFilename()
        self.getStandard(LAYERS_KEY).setValue([layerNum], tileset)

    def setWidth(self, NewX, layerNum):
        if layerNum >= self.getNumOfLayers():
            return
        oldX = self.getStandard(WIDTH_KEY).getValue([layerNum])
        self.getStandard(WIDTH_KEY).setValue([layerNum], NewX)
        self.updateTiles(layerNum, oldX, self.getStandard(HEIGHT_KEY).getValue([layerNum]))

    def setHeight(self, NewY, layerNum):
        if layerNum >= self.getNumOfLayers():
            return
        oldY = self.getStandard(HEIGHT_KEY).getValue([layerNum])
        self.getStandard(HEIGHT_KEY).setValue([layerNum], NewY)
        self.updateTiles(layerNum, self.getStandard(WIDTH_KEY).getValue([layerNum]), oldY)

    def setLayerNum(self, NewNum):
        self.getStandard(WIDTH_KEY).setSize([], NewNum)
        self.getStandard(HEIGHT_KEY).setSize([], NewNum)
        self.getStandard(LAYERS_KEY).setSize([], NewNum)
        self.getStandard(TILES_KEY).setSize([], NewNum)
        self.getStandard(NUM_OF_LAYERS_KEY).setValue(NewNum)


    def removeTileset(self, TilesetNum):
        if TilesetNum >= self.getNumOfLayers():
            return
        self.setLayer(TilesetNum, "")

    def updateTiles(self, layerNum, oldX, oldY):
        if layerNum >= self.getNumOfLayers():
            return
        Tileset = rgeTileset.TilesetData()
        Tileset.loadFromFile(self.getStandard(LAYERS_KEY).getValue([layerNum]))
        DefaultTile = Tileset.getDefaultTile()
        Tiles = [DefaultTile for i in xrange(self.getStandard(WIDTH_KEY).getValue([layerNum]) * self.getStandard(HEIGHT_KEY).getValue([layerNum]))]
        for i in range(oldX * oldY):
            xPos = i % oldX
            yPos = int(i / oldX)
            if xPos >= self.getStandard(WIDTH_KEY).getValue([layerNum]):
                continue
            if yPos >= self.getStandard(HEIGHT_KEY).getValue([layerNum]):
                break
            Tiles[xPos + (yPos * self.getStandard(WIDTH_KEY).getValue([layerNum]))] = self.getStandard(TILES_KEY).getValue([layerNum, xPos + (yPos * oldX)])
        self.getStandard(TILES_KEY).setSize([layerNum], len(Tiles))
        for i in range(len(Tiles)):
            self.getStandard(TILES_KEY).setValue([layerNum, i], Tiles[i])

    def setTile(self, NewTileNum, Index, Layer):
        if Layer >= self.getNumOfLayers():
            return
        self.getStandard(TILES_KEY).setValue([Layer, Index], NewTileNum)

